import { createRouter, createWebHistory } from "vue-router";
import indexView from "../views/index.vue";
import { getToken } from "@/utils/auth";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: indexView,
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login.vue"),
    },
  ],
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = getToken();
  // 判断是否需要登录权限
  if (to.fullPath == "/") {
    if (!token) {
      ElMessage.error("请先登录");
      next("/login");
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
